// pages/xufei/xufei.js
const { taocan ,payment,renewPayment} = require("../../util/api")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab:1,
    memberId:"",
    aaa:0,
    heqId:"",
    recordId:"",
    device:{},
    packageId:"",
    payPrice:"",
    jine:"",
    etId:"",
    list:[],
    eqId:'',

  },
  selected(evt){
    console.log('a', evt.currentTarget.dataset.jine)
    this.setData({
      aaa:evt.currentTarget.dataset.myid,
      jine:evt.currentTarget.dataset.jine.actualFee,
      packageId:evt.currentTarget.dataset.jine.id
      
    })
    // var index = e.currentTarget.id; //获取当前点击的下标
    
    // for (let i = 0; i < this.data.aaa.length; i++) {
    //   if(index==i){
    //     this.data.aaa[i]=true;
    //   }
    // }
    
  }
  ,
  showtaocan(e){
    this.setData({
      tab:e.currentTarget.dataset.tab
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.data.heqId=options.heqId
    this.data.recordId=options.recordId
    this.data.etId=options.etId
    this.data.eqId=options.eqId
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      memberId:wx.getStorageSync('memberId')
     
    })
    let id={
      "memberId":this.data.memberId,
      "etId":this.data.etId,
      eqId:this.data.heqId
    }
    taocan(id).then(res=>{
      if(res.code==200){
      this.setData({
        list:res.data.packageList,
        jine:res.data.packageList[0].actualFee,
        packageId:res.data.packageList[this.data.aaa].id,
       // payPrice.res.data.packageList[this.data.aaa].payPrice
      })
    }else{
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 3000
      })
    }
      
    
    })

  },
  pay(){

    let par={
      etId:this.data.etId,
      payPrice:this.data.jine,
      heqId:this.data.heqId,
      recordId:this.data.recordId,
      packageId:this.data.packageId,
      memberId:this.data.memberId,
  }
  console.log(par)

 renewPayment(par).then(res=>{
  
      if(res.code == 200)
     {
       let p = {
        id:res.data.id,
        payType:0,
        type:1
       }
      payment(p).then(res=>{
       console.log(res)
        if(res.code==200){
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.sign,
          success(res){
            wx.showModal({
              title: '提示',
              content:'支付成功',
              showCancel:false,
              success (res) {
                wx.navigateBack({
                  delta: 1,
                })
              }
            })
          }
        })
       
      }else{
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 3000
          })

        }
       })}
     }
 
     )

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})